home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 498 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: news.sprintlink.net!datalytics!news
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Beginner C++ Programmer has 1 More Question
  5. Date: 4 Jan 1996 19:47:18 GMT
  6. Organization: Datalytics, Inc
  7. Message-ID: <4chas6$ldg@gold.datalytics.com>
  8. References: <4bskl1$ain@ixnews5.ix.netcom.com>
  9. NNTP-Posting-Host: pc071.datalytics.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. psilocyn@ix.netcom.com(Jeff W. ) wrote:
  16. [snip]
  17. >and stuff.  I have an array, ScoresArray[21], which I use to hold 21
  18. >high scores read in from a file.  What I need to do is a few things. 
  19. >1)if the player's score makes the high score list, insert it in the
  20. >appropriate position in ScoresArray[21] (0=highest score, 20=lowest
  21. >score), and then adjust the array so that the scores are shifted down. 
  22. >For example, if I had a smaller array, SmallArray[4], filled with:
  23. >100
  24. >90
  25. >80
  26. >70
  27. >And the player's score is 95, I want to insert 95 between 100 and 90,
  28. >place 100 back into SmallArray[0], 95 into SmallArray[1], 90 into [2],
  29. >and 80 into [3] while discarding 70.  Does anyone have any idea as to
  30. >how to go about doing this?  Thanks.
  31.  
  32. With so few elements in the array, it may not be appropriate 
  33. to implement a binary search algorithm.  Therefore, I'll 
  34. concentrate on the brute force, sequential search approach.
  35.  
  36. const size_t MAX_SCORES = 21;
  37. size_t vScores[MAX_SCORES];
  38.  
  39. for (size_t i = 0; i < MAX_SCORES; i++)
  40. {
  41.     if (vScores[i] < score)
  42.     {
  43.         for (size_t j = MAX_SCORES - 1; j > i; j--)
  44.         {
  45.             vScores[j] = vScores[j - 1];
  46.         }
  47.         vScores[i] = score;
  48.     }
  49. }
  50.  
  51. This assumes you initialize the array to all zeroes so the 
  52. initial MAX_SCORES scores greater than zero will be inserted.
  53.  
  54. -- 
  55. Robert Stewart        | My opinions are usually my own.
  56. Datalytics, Inc.
  57. (513)226-7700
  58. stew@datalytics.com
  59.  
  60.  
  61.